package top.onedream.buildsmallsoftware.cfg.exception

import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler

@ControllerAdvice
class CustomExceptionAdvice {


    /**
     * 自定义异常处理
     * @param [ex] 自定义异常
     * @return [ResponseEntity<ExportedBean>]
     */
    @ExceptionHandler
    fun handle(ex: CustomException): ResponseEntity<ExportedBean> {
        ex.printStackTrace()
        return ResponseEntity(
            ExportedBean(ex.code, ex.message, ex.data),
            HttpStatus.INTERNAL_SERVER_ERROR
        )
    }


    /**
     * 全局异常处理
     * @param [ex] 全局异常
     * @return [ResponseEntity<ExportedBean>]
     */
    @ExceptionHandler
    fun exHandler(ex: Exception): ResponseEntity<ExportedBean> {
        ex.printStackTrace()
        return ResponseEntity(
            ExportedBean(500, ex.message, null),
            HttpStatus.INTERNAL_SERVER_ERROR
        )
    }

}